package com.lbs.jiepiaisi.manager;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.bean.EquipmentEntity;
import com.lbs.jiepiaisi.bean.TracingBean;
import com.lbs.jiepiaisi.bean.TrackEntity;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
 *
 * 时间：2017/9/15 14:50
 * 作用：实时跟踪地图管理类
 */
public class TrackPoiOverlay {

    private static LatLonPoint latLotPoint;
    private List<PoiItem> poiItems = new ArrayList<>();

    private AMap aMap;

    List<TracingBean.Result> results;

    List<BitmapDescriptor> bitmapDescriptor;

    private int padding;

    private static TrackPoiOverlay trackPoiOverlay = null;

    public SharePreferenceManager mSharePreference = null;

    public TrackPoiOverlay(AMap var1, List<TracingBean.Result> results, List<BitmapDescriptor> b, int padding) {
        this.aMap = var1;
        this.results = results;
        this.bitmapDescriptor = b;
        this.padding = padding;
    }

    //获得单例对象
    public static TrackPoiOverlay instance(AMap var1,
                                           List<TracingBean.Result> results,
                                           List<BitmapDescriptor> b,
                                           int padding){
        if(trackPoiOverlay == null){
            trackPoiOverlay= new TrackPoiOverlay(var1, results, b, padding);
        }
        trackPoiOverlay.aMap = var1;
        trackPoiOverlay.results = results;
        trackPoiOverlay.bitmapDescriptor = b;
        trackPoiOverlay.padding = padding;


        // results.size()的长度永远是1，所以说值取第0个
        Double lat=null;
        Double lng=null;
        if(results != null && results.size() > 0){
            lat=results.get(0).getGdLat();
            lng=results.get(0).getGdLng();
        }

        if(lat!=null&&lng!=null){
            latLotPoint = new LatLonPoint(lat,lng);
            PoiItem mPoiItem = new PoiItem(null, latLotPoint, null, null);
            trackPoiOverlay.poiItems.add(mPoiItem);

        }

        return  trackPoiOverlay;
    }

    /**
     * 添加Mark到地图中
     */
    Marker marker = null;

    public void addToMap() {
        if(marker != null){
            marker.remove();
        }
        marker  = aMap.addMarker(getMarker(0));
    }

    public Marker getMarker(){
        return marker;
    }


    /**
     * 移动镜头到当前的视角。
     */
    public void zoomToSpan() {
        try {
            if (this.poiItems != null && this.poiItems.size() > 0) {
                if (this.aMap == null) {
                    return;
                }

                if (this.poiItems.size() == 1) {
                    this.aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(this.poiItems.get(0).getLatLonPoint().getLatitude(), this.poiItems.get(0).getLatLonPoint().getLongitude()), 13.0F));
                } else {
                    LatLngBounds var1 = this.getAllMarkers();
                    this.aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(var1, padding));
                }
            }
        } catch (Throwable var2) {
            var2.printStackTrace();
        }

    }

    private LatLngBounds getAllMarkers() {
        LatLngBounds.Builder var1 = LatLngBounds.builder();
        for (int var2 = 0; var2 < this.poiItems.size(); ++var2) {
            var1.include(new LatLng(poiItems.get(var2).getLatLonPoint().getLatitude(), poiItems.get(var2).getLatLonPoint().getLongitude()));
        }

        return var1.build();
    }

    private MarkerOptions getMarker(int var1) {
        if(bitmapDescriptor != null && bitmapDescriptor.size() > 0){
            MarkerOptions marker = new MarkerOptions().anchor(0.5f, 0.5f)
                    .title(""+ var1)
                    .icon(bitmapDescriptor.get(var1));

            marker.position(new LatLng(latLotPoint.getLatitude(),latLotPoint.getLongitude()));
            return marker;

        }
        return null;
    }



    protected BitmapDescriptor getBitmapDescriptor(int var1) {
        return null;
    }

    protected String getTitle(int var1) {
        return this.poiItems.get(var1).getTitle();
    }

    protected String getSnippet(int var1) {
        return this.poiItems.get(var1).getSnippet();
    }



    /**
     * 返回第index的poi的信息。
     */
    public PoiItem getPoiItem(int var1) {
        return var1 >= 0 && var1 < this.poiItems.size() ? this.poiItems.get(var1) : null;
    }
    /**
     * 为当前Mark点设置图标
     */
    public static List<BitmapDescriptor> getBitmap(List<TracingBean.Result> results, Context mContext
            , String name/**车主姓名*/, String status/**设备状态*/) {

        List<BitmapDescriptor> mList = new ArrayList<>();
        for (int i = 0; i < results.size(); i++) {
            View view = View.inflate(mContext, R.layout.layout_marker, null);
            ImageView iv = (ImageView) view.findViewById(R.id.iv_marker);
            TextView tv_carname = (TextView) view.findViewById(R.id.tv_carname);
            tv_carname.setText(name);
//            if(status.equals("2")){
//                iv.setImageResource(R.mipmap.icon_hui_daohang);
//            }else{
                iv.setImageResource(R.mipmap.icon_daohang);
//            }
            iv.setRotation(results.get(i).getDir());
            Bitmap bitmap = convertViewToBitmap(view);
            mList.add(BitmapDescriptorFactory.fromBitmap(bitmap));
        }

        return mList;
    }
    /**
     * 将View转换为Bitmap的方法
     */
    //view 转bitmap

    public static Bitmap convertViewToBitmap(View view) {

        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.buildDrawingCache();

        Bitmap bitmap = view.getDrawingCache();

        return bitmap;

    }

}
